package com.ruoyi.demo.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.mapper.AuctionRecordMapper;
import com.ruoyi.demo.domain.AuctionRecord;
import com.ruoyi.demo.domain.dto.AuctionRecordDetailDTO;
import com.ruoyi.demo.service.IAuctionRecordService;
import com.ruoyi.demo.service.IAuctionUserService;

/**
 * 竞拍记录Service业务层处理
 * 
 * @author metaee
 * @date 2025-02-24
 */
@Service
public class AuctionRecordServiceImpl implements IAuctionRecordService 
{
    @Autowired
    private AuctionRecordMapper auctionRecordMapper;
    
    @Autowired
    private IAuctionUserService userService;

    /**
     * 查询竞拍记录
     * 
     * @param recordId 竞拍记录主键
     * @return 竞拍记录
     */
    @Override
    public AuctionRecord selectAuctionRecordByRecordId(Long recordId)
    {
        return auctionRecordMapper.selectAuctionRecordByRecordId(recordId);
    }

    /**
     * 查询竞拍记录列表
     * 
     * @param auctionRecord 竞拍记录
     * @return 竞拍记录
     */
    @Override
    public List<AuctionRecord> selectAuctionRecordList(AuctionRecord auctionRecord)
    {
        return auctionRecordMapper.selectAuctionRecordList(auctionRecord);
    }

    /**
     * 新增竞拍记录
     * 
     * @param auctionRecord 竞拍记录
     * @return 结果
     */
    @Override
    public int insertAuctionRecord(AuctionRecord auctionRecord)
    {
        auctionRecord.setCreateTime(DateUtils.getNowDate());
        return auctionRecordMapper.insertAuctionRecord(auctionRecord);
    }

    /**
     * 修改竞拍记录
     * 
     * @param auctionRecord 竞拍记录
     * @return 结果
     */
    @Override
    public int updateAuctionRecord(AuctionRecord auctionRecord)
    {
        auctionRecord.setUpdateTime(DateUtils.getNowDate());
        return auctionRecordMapper.updateAuctionRecord(auctionRecord);
    }

    /**
     * 批量删除竞拍记录
     * 
     * @param recordIds 需要删除的竞拍记录主键
     * @return 结果
     */
    @Override
    public int deleteAuctionRecordByRecordIds(Long[] recordIds)
    {
        return auctionRecordMapper.deleteAuctionRecordByRecordIds(recordIds);
    }

    /**
     * 删除竞拍记录信息
     * 
     * @param recordId 竞拍记录主键
     * @return 结果
     */
    @Override
    public int deleteAuctionRecordByRecordId(Long recordId)
    {
        return auctionRecordMapper.deleteAuctionRecordByRecordId(recordId);
    }

    /**
     * 根据商品ID查询竞拍记录列表，包括用户信息
     * 
     * @param goodsId 商品ID
     * @return 竞拍记录详情集合
     */
    @Override
    public List<AuctionRecordDetailDTO> selectAuctionRecordListByGoodsId(Long goodsId)
    {
        // 创建查询条件
        AuctionRecord queryParam = new AuctionRecord();
        queryParam.setGoodsId(goodsId);
        
        // 查询竞拍记录列表
        List<AuctionRecord> recordList = auctionRecordMapper.selectAuctionRecordList(queryParam);
        if (recordList == null || recordList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 转换为DTO对象并查询用户信息
        return recordList.stream().map(record -> {
            AuctionRecordDetailDTO detailDTO = new AuctionRecordDetailDTO();
            BeanUtils.copyProperties(record, detailDTO);
            
            // 查询用户信息
            if (record.getUserId() != null) {
                detailDTO.setUser(userService.selectAuctionUserByUserId(record.getUserId()));
            }
            
            return detailDTO;
        }).collect(Collectors.toList());
    }
}
